package data;

import java.util.List;

/**
 * Singleton class that can be called from anywhere.
 * Generates data upon first call.
 * 
 * Facade for utility classes that gets information about games and players.
 * Immutable
 * @author poet
 */
public class DataManager implements DataModel{
	private DataStorage dataStorage;
	public DataManager(DataGenerator generator){
		this.dataStorage = generator.getDataStorage();
	}
	/**
	 * defensive copying
	 * 
	 * @return
	 */
	public Player[] getPlayers() {
		return dataStorage.getPlayers();
	}

	/**
	 * defensive copying
	 * 
	 * @return
	 */
	public Game[] getGames() {
		//
		return dataStorage.getGames();
	}
	//Utility Methods
	
	//TODO migrate out, and keep this is a facade.
	public int getNumberOfGamesBetweenPlayers(Player player1, Player player2){
		List<Game> games = dataStorage.getPlayersToGamesMap().get(player1);
		int count = 0;
		for (Game game : games) {
			if (game.getPlayer1().equals(player2) || game.getPlayer2().equals(player2))
				count++;
		}
		return count;
	}
	public int getNumberOfGames(Player player){
		return dataStorage.getPlayersToGamesMap().get(player).size();
	}
	


}
